
import ParserError from "@/js/GraphParser/src/ParserError"
import { DefaultLayout, GraphNode } from "@/js/components/GraphNode"
import { GraphView } from "@/js/components/GraphView"
import { InputLinkNodeAny, InputLinkNodeNumber, InputLinkNodeString } from "@/js/components/InputLinkNode"

export class Let extends GraphNode{
    layout?: DefaultLayout
    load(){
        super.load()

        const layout = new this.DefaultLayout(this, this.graphView as GraphView)
        this.layout = layout
        this.appendChild(layout.container)

        const stringEntry = new InputLinkNodeString(this, this.graphView as GraphView, {
            name: "varName",
            desc: '变量名称',
            placeholder:"变量名称",
            model: "entry",
            showLinkNode: false
        })
        layout.main?.left.appendChild(stringEntry)

        // 默认值
        const defauleEntry = new InputLinkNodeAny(this, this.graphView as GraphView, {
            name: "defaultValue",
            desc: '默认值',
            model: "entry",
            placeholder: "默认值"
        })
        layout.main?.left.appendChild(defauleEntry)

        layout.main?.right.appendChild(new InputLinkNodeAny(this, this.graphView as GraphView, {
            name: "quote", 
            desc: '引用',
            model: "output",
            showInput: false,
            linkMaxCount: 100000
        }))
    }
}

Let.parser = ({typeConversion, node, to, next, regExpHelper})=>{
    const enters = node.userData.enters
    
    if(!enters.varName || enters.varName.trim() == "") throw new ParserError(node.uuid, "！错误：未进行变量命名")

    if(!regExpHelper.isValidVariableName( enters.varName )) throw new ParserError(node.uuid, "！错误：变量名称格式不正确")

    if(to.name === "enter") {
        const defaultValue = next(node.enters.defaultValue, typeConversion(enters.defaultValue))
        return `let ${enters.varName}${(defaultValue && '=' + defaultValue )|| ''};${next(node.outputs.output)}`
    }
    else if(to.name === "quote"){
        return enters.varName
    }

    return ''
}